package com.sxp.service;

import com.sxp.dao.FoodSelectionDaoImpl;

import com.sxp.dao.MealDaoImpl;
import com.sxp.dao.RiceDao;
import com.sxp.dao.RiceDaoImpl;
import com.sxp.model.FoodSelection;
import com.sxp.model.MealType;
import com.sxp.model.Month;
import com.sxp.model.MonthlyCount;


import java.util.*;

/**
 * Created by lakmini on 10/16/14.
 */
public class MonthlyDetailService{

    public final FoodSelectionDaoImpl foodSelectionDaoImpl;

    public MealDaoImpl mD;



    public MonthlyDetailService() {

        foodSelectionDaoImpl = new FoodSelectionDaoImpl();

        mD=new MealDaoImpl();


    }

    public TreeMap getCurrentMonthPopulatedMap(TreeMap map){
        Calendar cal = Calendar.getInstance();
        Month month=new Month();
        int maxDay=month.getCurrentMonthMaximumDays();
        for (int i=1; i<=maxDay;i++){
            //if(map.containsKey(i)){
            if(!(map.containsValue(map.get(i)))){
                map.put(i, 0);
            }
            // }
        }
        return map;
    }
    public TreeMap getPreviousMonthPopulatedMap(TreeMap map){
        Calendar cal = Calendar.getInstance();
        Month month=new Month();
        int maxDay=month.getPreviousMonthMaximumDays();
        for (int i=1; i<=maxDay;i++){
            //if(map.containsKey(i)){
            if(!(map.containsValue(map.get(i)))){
                map.put(i, 0);
            }
            // }
        }
        return map;
    }
    public TreeMap getNextMonthPopulatedMap(TreeMap map){
        Calendar cal = Calendar.getInstance();
        Month month=new Month();
        int maxDay=month.getNextMonthMaximumDays();
        for (int i=1; i<=maxDay;i++){
            //if(map.containsKey(i)){
            if(!(map.containsValue(map.get(i)))){
                map.put(i, 0);
            }
            // }
        }
        return map;
    }

    public Map<String,TreeMap<Integer,Integer> >  makeCurrentMonthMealDisplayCount(HashMap<String,TreeMap<Integer,Integer>> mealTypeCountByDateMap){
        HashMap<String,TreeMap<Integer,Integer>> newMealTypeCountByDateMap  = new HashMap<String, TreeMap<Integer, Integer>>();

        Iterator itr = mealTypeCountByDateMap.entrySet().iterator();
        while(itr.hasNext()){
            Map.Entry entry = (Map.Entry)itr.next();
            newMealTypeCountByDateMap.put(entry.getKey().toString(), getCurrentMonthPopulatedMap((TreeMap)entry.getValue()));
        }

        return newMealTypeCountByDateMap;
    }

    public Map<String,TreeMap<Integer,Integer> >  makePreviousMonthMealDisplayCount(HashMap<String,TreeMap<Integer,Integer>> mealTypeCountByDateMap){
        HashMap<String,TreeMap<Integer,Integer>> newMealTypeCountByDateMap  = new HashMap<String, TreeMap<Integer, Integer>>();

        Iterator itr = mealTypeCountByDateMap.entrySet().iterator();
        while(itr.hasNext()){
            Map.Entry entry = (Map.Entry)itr.next();
            newMealTypeCountByDateMap.put(entry.getKey().toString(), getPreviousMonthPopulatedMap((TreeMap)entry.getValue()));
        }

        return newMealTypeCountByDateMap;
    }

    public Map<String,TreeMap<Integer,Integer> >  makeNextMonthMealDisplayCount(HashMap<String,TreeMap<Integer,Integer>> mealTypeCountByDateMap){
        HashMap<String,TreeMap<Integer,Integer>> newMealTypeCountByDateMap  = new HashMap<String, TreeMap<Integer, Integer>>();

        Iterator itr = mealTypeCountByDateMap.entrySet().iterator();
        while(itr.hasNext()){
            Map.Entry entry = (Map.Entry)itr.next();
            newMealTypeCountByDateMap.put(entry.getKey().toString(), getNextMonthPopulatedMap((TreeMap)entry.getValue()));
        }

        return newMealTypeCountByDateMap;
    }



    public Map<String,TreeMap<Integer,Integer> > getCurrentMonthMealCountMap() {
        List<MonthlyCount> selectedMealList=foodSelectionDaoImpl.getCurrentMonthMealCount();

        Map<String,TreeMap<Integer,Integer> > mealTypeCountByDateMap = new HashMap<String, TreeMap<Integer,Integer>>();

        Iterator<MonthlyCount> selectedMealListIterator=selectedMealList.iterator();

        while(selectedMealListIterator.hasNext()) {
            MonthlyCount monthlyCount = selectedMealListIterator.next();

            if (monthlyCount.getMeal_id() == 1){
                String mealName = monthlyCount.getMealName();
                TreeMap<Integer, Integer> chickenCountByDateMap = mealTypeCountByDateMap.get(mealName);
                chickenCountByDateMap = (chickenCountByDateMap == null ? new TreeMap<Integer, Integer>() : chickenCountByDateMap); //create new if it doesn't exist
                chickenCountByDateMap.put(monthlyCount.getDay(), monthlyCount.getCount()); // Add this combination of day / count for the specific meal type map
                mealTypeCountByDateMap.put(mealName, chickenCountByDateMap);// Put the updated map back
            }
            if (monthlyCount.getMeal_id() == 2){
                String mealName = monthlyCount.getMealName();
                TreeMap<Integer, Integer> eggCountByDateMap = mealTypeCountByDateMap.get(mealName);
                eggCountByDateMap = (eggCountByDateMap == null ? new TreeMap<Integer, Integer>() : eggCountByDateMap); //create new if it doesn't exist
                eggCountByDateMap.put(monthlyCount.getDay(), monthlyCount.getCount()); // Add this combination of day / count for the specific meal type map
                mealTypeCountByDateMap.put(mealName, eggCountByDateMap);// Put the updated map back
            }

            if (monthlyCount.getMeal_id() == 3){
                String mealName = monthlyCount.getMealName();
                TreeMap<Integer, Integer> fishCountByDateMap = mealTypeCountByDateMap.get(mealName);
                fishCountByDateMap = (fishCountByDateMap == null ? new TreeMap<Integer, Integer>() : fishCountByDateMap); //create new if it doesn't exist
                fishCountByDateMap.put(monthlyCount.getDay(), monthlyCount.getCount()); // Add this combination of day / count for the specific meal type map
                mealTypeCountByDateMap.put(mealName, fishCountByDateMap);// Put the updated map back
            }

            if (monthlyCount.getMeal_id() == 4){
                String mealName = monthlyCount.getMealName();
                TreeMap<Integer, Integer> vegeCountByDateMap = mealTypeCountByDateMap.get(mealName);
                vegeCountByDateMap = (vegeCountByDateMap == null ? new TreeMap<Integer, Integer>() : vegeCountByDateMap); //create new if it doesn't exist
                vegeCountByDateMap.put(monthlyCount.getDay(), monthlyCount.getCount()); // Add this combination of day / count for the specific meal type map
                mealTypeCountByDateMap.put(mealName, vegeCountByDateMap);// Put the updated map back
            }


        }

        return makeCurrentMonthMealDisplayCount((HashMap<String, TreeMap<Integer, Integer>>) mealTypeCountByDateMap);

    }


    public Map<String,TreeMap<Integer,Integer> > getPreviousMonthMealCountMap() {
        List<MonthlyCount> selectedMealList=foodSelectionDaoImpl.getPreviousMonthMealCount();

        Map<String,TreeMap<Integer,Integer> > mealTypeCountByDateMap = new HashMap<String, TreeMap<Integer,Integer>>();

        Iterator<MonthlyCount> selectedMealListIterator=selectedMealList.iterator();

        while(selectedMealListIterator.hasNext()) {
            MonthlyCount monthlyCount = selectedMealListIterator.next();

            if (monthlyCount.getMeal_id() == 1){
                String mealName = monthlyCount.getMealName();
                TreeMap<Integer, Integer> chickenCountByDateMap = mealTypeCountByDateMap.get(mealName);
                chickenCountByDateMap = (chickenCountByDateMap == null ? new TreeMap<Integer, Integer>() : chickenCountByDateMap); //create new if it doesn't exist
                chickenCountByDateMap.put(monthlyCount.getDay(), monthlyCount.getCount()); // Add this combination of day / count for the specific meal type map
                mealTypeCountByDateMap.put(mealName, chickenCountByDateMap);// Put the updated map back
            }
            if (monthlyCount.getMeal_id() == 2){
                String mealName = monthlyCount.getMealName();
                TreeMap<Integer, Integer> eggCountByDateMap = mealTypeCountByDateMap.get(mealName);
                eggCountByDateMap = (eggCountByDateMap == null ? new TreeMap<Integer, Integer>() : eggCountByDateMap); //create new if it doesn't exist
                eggCountByDateMap.put(monthlyCount.getDay(), monthlyCount.getCount()); // Add this combination of day / count for the specific meal type map
                mealTypeCountByDateMap.put(mealName, eggCountByDateMap);// Put the updated map back
            }

            if (monthlyCount.getMeal_id() == 3){
                String mealName = monthlyCount.getMealName();
                TreeMap<Integer, Integer> fishCountByDateMap = mealTypeCountByDateMap.get(mealName);
                fishCountByDateMap = (fishCountByDateMap == null ? new TreeMap<Integer, Integer>() : fishCountByDateMap); //create new if it doesn't exist
                fishCountByDateMap.put(monthlyCount.getDay(), monthlyCount.getCount()); // Add this combination of day / count for the specific meal type map
                mealTypeCountByDateMap.put(mealName, fishCountByDateMap);// Put the updated map back
            }

            if (monthlyCount.getMeal_id() == 4){
                String mealName = monthlyCount.getMealName();
                TreeMap<Integer, Integer> vegeCountByDateMap = mealTypeCountByDateMap.get(mealName);
                vegeCountByDateMap = (vegeCountByDateMap == null ? new TreeMap<Integer, Integer>() : vegeCountByDateMap); //create new if it doesn't exist
                vegeCountByDateMap.put(monthlyCount.getDay(), monthlyCount.getCount()); // Add this combination of day / count for the specific meal type map
                mealTypeCountByDateMap.put(mealName, vegeCountByDateMap);// Put the updated map back
            }


        }

        return makePreviousMonthMealDisplayCount((HashMap<String, TreeMap<Integer, Integer>>) mealTypeCountByDateMap);

    }
    public Map<String,TreeMap<Integer,Integer> > getNextMonthMealCountMap() {
        List<MonthlyCount> selectedMealList=foodSelectionDaoImpl.getNextMonthMealCount();

        Map<String,TreeMap<Integer,Integer> > mealTypeCountByDateMap = new HashMap<String, TreeMap<Integer,Integer>>();

        Iterator<MonthlyCount> selectedMealListIterator=selectedMealList.iterator();

        while(selectedMealListIterator.hasNext()) {
            MonthlyCount monthlyCount = selectedMealListIterator.next();

            if (monthlyCount.getMeal_id() == 1){
                String mealName = monthlyCount.getMealName();
                TreeMap<Integer, Integer> chickenCountByDateMap = mealTypeCountByDateMap.get(mealName);
                chickenCountByDateMap = (chickenCountByDateMap == null ? new TreeMap<Integer, Integer>() : chickenCountByDateMap); //create new if it doesn't exist
                chickenCountByDateMap.put(monthlyCount.getDay(), monthlyCount.getCount()); // Add this combination of day / count for the specific meal type map
                mealTypeCountByDateMap.put(mealName, chickenCountByDateMap);// Put the updated map back
            }
            if (monthlyCount.getMeal_id() == 2){
                String mealName = monthlyCount.getMealName();
                TreeMap<Integer, Integer> eggCountByDateMap = mealTypeCountByDateMap.get(mealName);
                eggCountByDateMap = (eggCountByDateMap == null ? new TreeMap<Integer, Integer>() : eggCountByDateMap); //create new if it doesn't exist
                eggCountByDateMap.put(monthlyCount.getDay(), monthlyCount.getCount()); // Add this combination of day / count for the specific meal type map
                mealTypeCountByDateMap.put(mealName, eggCountByDateMap);// Put the updated map back
            }

            if (monthlyCount.getMeal_id() == 3){
                String mealName = monthlyCount.getMealName();
                TreeMap<Integer, Integer> fishCountByDateMap = mealTypeCountByDateMap.get(mealName);
                fishCountByDateMap = (fishCountByDateMap == null ? new TreeMap<Integer, Integer>() : fishCountByDateMap); //create new if it doesn't exist
                fishCountByDateMap.put(monthlyCount.getDay(), monthlyCount.getCount()); // Add this combination of day / count for the specific meal type map
                mealTypeCountByDateMap.put(mealName, fishCountByDateMap);// Put the updated map back
            }

            if (monthlyCount.getMeal_id() == 4){
                String mealName = monthlyCount.getMealName();
                TreeMap<Integer, Integer> vegeCountByDateMap = mealTypeCountByDateMap.get(mealName);
                vegeCountByDateMap = (vegeCountByDateMap == null ? new TreeMap<Integer, Integer>() : vegeCountByDateMap); //create new if it doesn't exist
                vegeCountByDateMap.put(monthlyCount.getDay(), monthlyCount.getCount()); // Add this combination of day / count for the specific meal type map
                mealTypeCountByDateMap.put(mealName, vegeCountByDateMap);// Put the updated map back
            }


        }

        return makeNextMonthMealDisplayCount((HashMap<String, TreeMap<Integer, Integer>>) mealTypeCountByDateMap);

    }



    public Map<String,TreeMap<Integer,Integer> > getCurrentMonthRiceCountMap() {
        List<MonthlyCount> selectedRiceList=foodSelectionDaoImpl.getCurrentMonthRiceCount();

        Map<String,TreeMap<Integer,Integer> > riceTypeCountByDateMap = new HashMap<String, TreeMap<Integer,Integer>>();

        Iterator<MonthlyCount> selectedRiceListIterator=selectedRiceList.iterator();

        while(selectedRiceListIterator.hasNext()) {
            MonthlyCount monthlyCount = selectedRiceListIterator.next();

            if (monthlyCount.getRice_id() == 0){
                String riceName = monthlyCount.getRiceName();
                TreeMap<Integer, Integer> nonCountByDateMap = riceTypeCountByDateMap.get(riceName);
                nonCountByDateMap = (nonCountByDateMap == null ? new TreeMap<Integer, Integer>() : nonCountByDateMap); //create new if it doesn't exist
                nonCountByDateMap.put(monthlyCount.getDay(), monthlyCount.getCount()); // Add this combination of day / count for the specific meal type map
                riceTypeCountByDateMap.put(riceName, nonCountByDateMap);// Put the updated map back
            }
            if (monthlyCount.getRice_id() == 1){
                String riceName = monthlyCount.getRiceName();
                TreeMap<Integer, Integer> whiteRiceCountByDateMap = riceTypeCountByDateMap.get(riceName);
                whiteRiceCountByDateMap = (whiteRiceCountByDateMap == null ? new TreeMap<Integer, Integer>() : whiteRiceCountByDateMap); //create new if it doesn't exist
                whiteRiceCountByDateMap.put(monthlyCount.getDay(), monthlyCount.getCount()); // Add this combination of day / count for the specific meal type map
                riceTypeCountByDateMap.put(riceName, whiteRiceCountByDateMap);// Put the updated map back
            }

            if (monthlyCount.getRice_id() == 2){
                String riceName = monthlyCount.getRiceName();
                TreeMap<Integer, Integer> redRiceCountByDateMap = riceTypeCountByDateMap.get(riceName);
                redRiceCountByDateMap = (redRiceCountByDateMap == null ? new TreeMap<Integer, Integer>() : redRiceCountByDateMap); //create new if it doesn't exist
                redRiceCountByDateMap.put(monthlyCount.getDay(), monthlyCount.getCount()); // Add this combination of day / count for the specific meal type map
                riceTypeCountByDateMap.put(riceName, redRiceCountByDateMap);// Put the updated map back
            }


        }

        return makeCurrentMonthMealDisplayCount((HashMap<String, TreeMap<Integer, Integer>>) riceTypeCountByDateMap);

    }

    public Map<String,TreeMap<Integer,Integer> > getPreviousMonthRiceCountMap() {
        List<MonthlyCount> selectedRiceList=foodSelectionDaoImpl.getPreviousMonthRiceCount();

        Map<String,TreeMap<Integer,Integer> > riceTypeCountByDateMap = new HashMap<String, TreeMap<Integer,Integer>>();

        Iterator<MonthlyCount> selectedRiceListIterator=selectedRiceList.iterator();

        while(selectedRiceListIterator.hasNext()) {
            MonthlyCount monthlyCount = selectedRiceListIterator.next();

            if (monthlyCount.getRice_id() == 0){
                String riceName = monthlyCount.getRiceName();
                TreeMap<Integer, Integer> nonCountByDateMap = riceTypeCountByDateMap.get(riceName);
                nonCountByDateMap = (nonCountByDateMap == null ? new TreeMap<Integer, Integer>() : nonCountByDateMap); //create new if it doesn't exist
                nonCountByDateMap.put(monthlyCount.getDay(), monthlyCount.getCount()); // Add this combination of day / count for the specific meal type map
                riceTypeCountByDateMap.put(riceName, nonCountByDateMap);// Put the updated map back
            }
            if (monthlyCount.getRice_id() == 1){
                String riceName = monthlyCount.getRiceName();
                TreeMap<Integer, Integer> whiteRiceCountByDateMap = riceTypeCountByDateMap.get(riceName);
                whiteRiceCountByDateMap = (whiteRiceCountByDateMap == null ? new TreeMap<Integer, Integer>() : whiteRiceCountByDateMap); //create new if it doesn't exist
                whiteRiceCountByDateMap.put(monthlyCount.getDay(), monthlyCount.getCount()); // Add this combination of day / count for the specific meal type map
                riceTypeCountByDateMap.put(riceName, whiteRiceCountByDateMap);// Put the updated map back
            }

            if (monthlyCount.getRice_id() == 2){
                String riceName = monthlyCount.getRiceName();
                TreeMap<Integer, Integer> redRiceCountByDateMap = riceTypeCountByDateMap.get(riceName);
                redRiceCountByDateMap = (redRiceCountByDateMap == null ? new TreeMap<Integer, Integer>() : redRiceCountByDateMap); //create new if it doesn't exist
                redRiceCountByDateMap.put(monthlyCount.getDay(), monthlyCount.getCount()); // Add this combination of day / count for the specific meal type map
                riceTypeCountByDateMap.put(riceName, redRiceCountByDateMap);// Put the updated map back
            }


        }

        return makePreviousMonthMealDisplayCount((HashMap<String, TreeMap<Integer, Integer>>) riceTypeCountByDateMap);

    }

    public Map<String,TreeMap<Integer,Integer> > getNextMonthRiceCountMap() {
        List<MonthlyCount> selectedRiceList=foodSelectionDaoImpl.getNextMonthRiceCount();

        Map<String,TreeMap<Integer,Integer> > riceTypeCountByDateMap = new HashMap<String, TreeMap<Integer,Integer>>();

        Iterator<MonthlyCount> selectedRiceListIterator=selectedRiceList.iterator();

        while(selectedRiceListIterator.hasNext()) {
            MonthlyCount monthlyCount = selectedRiceListIterator.next();

            if (monthlyCount.getRice_id() == 0){
                String riceName = monthlyCount.getRiceName();
                TreeMap<Integer, Integer> nonCountByDateMap = riceTypeCountByDateMap.get(riceName);
                nonCountByDateMap = (nonCountByDateMap == null ? new TreeMap<Integer, Integer>() : nonCountByDateMap); //create new if it doesn't exist
                nonCountByDateMap.put(monthlyCount.getDay(), monthlyCount.getCount()); // Add this combination of day / count for the specific meal type map
                riceTypeCountByDateMap.put(riceName, nonCountByDateMap);// Put the updated map back
            }
            if (monthlyCount.getRice_id() == 1){
                String riceName = monthlyCount.getRiceName();
                TreeMap<Integer, Integer> whiteRiceCountByDateMap = riceTypeCountByDateMap.get(riceName);
                whiteRiceCountByDateMap = (whiteRiceCountByDateMap == null ? new TreeMap<Integer, Integer>() : whiteRiceCountByDateMap); //create new if it doesn't exist
                whiteRiceCountByDateMap.put(monthlyCount.getDay(), monthlyCount.getCount()); // Add this combination of day / count for the specific meal type map
                riceTypeCountByDateMap.put(riceName, whiteRiceCountByDateMap);// Put the updated map back
            }

            if (monthlyCount.getRice_id() == 2){
                String riceName = monthlyCount.getRiceName();
                TreeMap<Integer, Integer> redRiceCountByDateMap = riceTypeCountByDateMap.get(riceName);
                redRiceCountByDateMap = (redRiceCountByDateMap == null ? new TreeMap<Integer, Integer>() : redRiceCountByDateMap); //create new if it doesn't exist
                redRiceCountByDateMap.put(monthlyCount.getDay(), monthlyCount.getCount()); // Add this combination of day / count for the specific meal type map
                riceTypeCountByDateMap.put(riceName, redRiceCountByDateMap);// Put the updated map back
            }


        }

        return makeNextMonthMealDisplayCount((HashMap<String, TreeMap<Integer, Integer>>) riceTypeCountByDateMap);

    }



    public TreeMap<Integer,Integer> getCurrentMonthTotalByDate(){
        List<MonthlyCount> currentMonthTotalList=foodSelectionDaoImpl.getCurrentMonthTotal();

        TreeMap<Integer,Integer>  currentMonthTotalMap = new TreeMap<Integer,Integer>();

        Iterator<MonthlyCount> currentMonthTotalListIterator=currentMonthTotalList.iterator();

        while(currentMonthTotalListIterator.hasNext()) {
            MonthlyCount monthlyCount = currentMonthTotalListIterator.next();
            currentMonthTotalMap.put(monthlyCount.getDay(),monthlyCount.getCount());

        }
        return getCurrentMonthPopulatedMap(currentMonthTotalMap);

    }

    public TreeMap<Integer,Integer> getPreviousMonthTotalByDate(){
        List<MonthlyCount> previousMonthTotalList=foodSelectionDaoImpl.getPreviousMonthTotal();

        TreeMap<Integer,Integer>  previousMonthTotalMap = new TreeMap<Integer,Integer>();

        Iterator<MonthlyCount> previousMonthTotalListIterator=previousMonthTotalList.iterator();

        while(previousMonthTotalListIterator.hasNext()) {
            MonthlyCount monthlyCount = previousMonthTotalListIterator.next();
            previousMonthTotalMap.put(monthlyCount.getDay(),monthlyCount.getCount());

        }
        return getPreviousMonthPopulatedMap(previousMonthTotalMap);

    }

    public TreeMap<Integer,Integer> getNextMonthTotalByDate(){
        List<MonthlyCount> nextMonthTotalList=foodSelectionDaoImpl.getNextMonthTotal();

        TreeMap<Integer,Integer>  nextMonthTotalMap = new TreeMap<Integer,Integer>();

        Iterator<MonthlyCount> nextMonthTotalListIterator=nextMonthTotalList.iterator();

        while(nextMonthTotalListIterator.hasNext()) {
            MonthlyCount monthlyCount = nextMonthTotalListIterator.next();
            nextMonthTotalMap.put(monthlyCount.getDay(),monthlyCount.getCount());

        }
        return getNextMonthPopulatedMap(nextMonthTotalMap);

    }
}
